package com.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.adapter.base.ViewHolder;
import com.adapter.base.recyclerview.CommonAdapter;
import com.util.R;
import com.view.MyLinearLayoutManager;
import com.view.SwipeItemLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/27.
 */
public class SwipeItemLayoutAcy extends BaseActivity {


    @Bind(R.id.rv_swipeItemLayoutAcy)
    RecyclerView recyclerView;
    private List<SwipeItemLayout> swipeItemLayoutList = new ArrayList<>();
    private List<String> list;
    private MyLinearLayoutManager myLinearLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acy_swipe_item_layout);
        ButterKnife.bind(this, this);
        initEvent();
        initData();
    }


    @Override
    protected void onDestroy() {

        super.onDestroy();
        ButterKnife.unbind(this);
    }

    @Override
    protected void initEvent() {
        super.initEvent();

    }

    @Override
    protected void initData() {
        super.initData();
         list= new ArrayList<>();
        list.add("滑动删除1");
        list.add("滑动删除2");
        list.add("滑动删除3");
        list.add("滑动删除4");
        list.add("滑动删除5");
        list.add("滑动删除6");
        list.add("滑动删除7");
        list.add("滑动删除8");
        list.add("滑动删除9");
        list.add("滑动删除10");
        list.add("滑动删除11");
        list.add("滑动删除12");
        list.add("滑动删除13");
        list.add("滑动删除14");
        list.add("滑动删除15");
        list.add("滑动删除16");
        list.add("滑动删除17");
        list.add("滑动删除18");
        list.add("滑动删除19");
        list.add("滑动删除20");
        list.add("滑动删除21");
        list.add("滑动删除22");
        list.add("滑动删除23");
        list.add("滑动删除24");
        list.add("滑动删除25");
        list.add("滑动删除26");
        list.add("滑动删除27");
        list.add("滑动删除28");
        list.add("滑动删除29");
        list.add("滑动删除30");
        myLinearLayoutManager = new MyLinearLayoutManager(SwipeItemLayoutAcy.this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(myLinearLayoutManager);
        recyclerView.setAdapter(new CommonAdapter<String>(SwipeItemLayoutAcy.this, R.layout.item_swipe_layout, list) {
            @Override
            public void convert(final ViewHolder holder, String s) {
                holder.setText(R.id.tv_content, s);
                final SwipeItemLayout swipeItemLayout = holder.getView(R.id.sil);
                if (cacheSwipeItemLayout(swipeItemLayout)) {
                    swipeItemLayout.setSwipeListener(new SwipeItemLayout.SwipeListener() {
                        @Override
                        public void onStartSwipeOpen() {
                            exchangeSwipeItemLayout(swipeItemLayout);
                        }

                        @Override
                        public void onSwiping() {

                        }

                        @Override
                        public void onSwipeOpenComplete() {

                        }

                        @Override
                        public void onSwipeCloseComplete() {

                        }
                    });

                    holder.setOnClickListener(R.id.tv_delete, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            swipeItemLayout.slideToClose();
                            list.remove(holder.getAdapterPosition());
                            notifyItemRemoved(holder.getAdapterPosition());
                        }
                    });
                }
            }
        });
    }

    //转换显示的Item，保证只显示一个
    private void exchangeSwipeItemLayout(SwipeItemLayout swipeItemLayout) {
//        LogUtil.i("test", "exchangeSwipeItemLayout");
        for (SwipeItemLayout s : swipeItemLayoutList) {
            if (s != swipeItemLayout) {
                s.slideToClose();
            }
        }
    }


    private boolean cacheSwipeItemLayout(SwipeItemLayout swipeItemLayout) {
        boolean isAddSuccess = true;
        if (swipeItemLayoutList.size() != 0) {
            for (SwipeItemLayout s : swipeItemLayoutList) {
                if (swipeItemLayout == s) {
                    isAddSuccess = false;
                }
            }
        } else {
            swipeItemLayoutList.add(swipeItemLayout);
            return isAddSuccess;
        }
        if (isAddSuccess) {
            swipeItemLayoutList.add(swipeItemLayout);
        }
        return isAddSuccess;
    }
}
